package org.jeecg.modules.product.risk.service.impl;

import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.product.risk.entity.TbBaseCheck;
import org.jeecg.modules.product.risk.entity.TbBaseCheckItem;
import org.jeecg.modules.product.risk.mapper.TbBaseCheckItemMapper;
import org.jeecg.modules.product.risk.mapper.TbBaseCheckMapper;
import org.jeecg.modules.product.risk.service.ITbBaseCheckService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 检查表
 * @Author: jeecg-boot
 * @Date:   2024-05-21
 * @Version: V1.0
 */
@Service
public class TbBaseCheckServiceImpl extends ServiceImpl<TbBaseCheckMapper, TbBaseCheck> implements ITbBaseCheckService {

	@Autowired
	private TbBaseCheckMapper tbBaseCheckMapper;
	@Autowired
	private TbBaseCheckItemMapper tbBaseCheckItemMapper;
	
	@Override
	@Transactional
	public void saveMain(TbBaseCheck tbBaseCheck, List<TbBaseCheckItem> tbBaseCheckItemList) {
		tbBaseCheck.setId(UUIDGenerator.generate());
		tbBaseCheckMapper.insert(tbBaseCheck);
		if(tbBaseCheckItemList!=null && tbBaseCheckItemList.size()>0) {
			for(TbBaseCheckItem entity:tbBaseCheckItemList) {
				//外键设置
				entity.setCheckId(tbBaseCheck.getId());
				entity.setId(UUIDGenerator.generate());
				tbBaseCheckItemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(TbBaseCheck tbBaseCheck,List<TbBaseCheckItem> tbBaseCheckItemList) {
		tbBaseCheckMapper.updateById(tbBaseCheck);
		
		//1.先删除子表数据
		tbBaseCheckItemMapper.deleteByMainId(tbBaseCheck.getId());
		
		//2.子表数据重新插入
		if(tbBaseCheckItemList!=null && tbBaseCheckItemList.size()>0) {
			for(TbBaseCheckItem entity:tbBaseCheckItemList) {
				//外键设置
				entity.setCheckId(tbBaseCheck.getId());
				entity.setId(UUIDGenerator.generate());
				tbBaseCheckItemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		tbBaseCheckItemMapper.deleteByMainId(id);
		tbBaseCheckMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			tbBaseCheckItemMapper.deleteByMainId(id.toString());
			tbBaseCheckMapper.deleteById(id);
		}
	}
	
}
